import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.Jedis;

public class redisMap<K, V> implements Map<Object, Object> {

	Jedis redisService;
	BinaryJedis binaryJedis;

	public redisMap() {
		redisService = new Jedis("localhost", 6379);
		
		binaryJedis = new BinaryJedis("localhost", 6379);
	}

	private byte[] objectToBytes(Object obj) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ObjectOutputStream sOut;
		try {
			sOut = new ObjectOutputStream(new ByteArrayOutputStream());
			sOut.writeObject(obj);
			sOut.flush();
			byte[] bytes = out.toByteArray();
			return bytes;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}
		return null;
	}

	public static Object bytesToObject(byte[] bytes) {

		// byte转object
		ByteArrayInputStream in = new ByteArrayInputStream(bytes);
		ObjectInputStream sIn;
		try {
			sIn = new ObjectInputStream(in);
			return sIn.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int size() {
		binaryJedis.dbSize().intValue();
		return redisService.dbSize().intValue();
	}

	@Override
	public boolean isEmpty() {
		return (redisService.dbSize() > 0);
	}

	@Override
	public boolean containsKey(Object key) {
		redisService.exists(objectToBytes(key));
		return false;
	}

	@Override
	public boolean containsValue(Object value) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Object get(Object key) {
		
		return bytesToObject(redisService.get(objectToBytes(key)));
	}

	@Override
	public Object put(Object key, Object value) {
		return bytesToObject(redisService.getSet(objectToBytes(key),
				objectToBytes(value)));
	}

	@Override
	public Object remove(Object key) {
		return redisService.del(objectToBytes(key));
	}

	@Override
	public void putAll(Map m) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clear() {
		redisService.flushDB();
	}

	@Override
	public Set<Object> keySet() {
		Set<byte[]> keys = redisService.keys(new byte[]{'*'});
		Set<Object> set = new HashSet<Object>();
		for (byte[] bs : keys) {
			set.add(bytesToObject(bs));
		}
		return set;
	}

	@Override
	public Collection<Object> values() {
		Set<byte[]> keys = redisService.keys(new byte[]{'*'});
		Set<Object> set = new HashSet<Object>();
		for (byte[] bs : keys) {
			set.add(bytesToObject(redisService.get(bs)));
		}
		return set;
	}

	@Override
	public Set entrySet() {
		// TODO Auto-generated method stub
		return null;
	}
}
